# Preliminaries ####
rm(list = ls())
library(tidyverse)

# S. Material #1: ####
# In the appendix, we provide a country-specific summary of our variables of interest, including negotiation frequency and militia activity by target.

# data ####
d <- read.csv("data_main_aan25_militias_neg.csv")
names(d)

## Country - Specific Graphs ####
# Main variables
d$x_reb <- d$Mil_vs_Reb_all # vs rebel  (x)
d$x_civ <- d$Mil_vs_Civ # vs civilian
d$x_gov <- d$Mil_vs_Gov # # vs gov
d$x_mil <- d$Mil_vs_Mil # vs other militia

# counter for rebel no:
d$reb_no <- 1

# summary stats:
dx <- d |> 
  select(
    SideA, 
    Year, 
    Negotiation, 
    Mediation, 
    x_reb, 
    x_civ,
    x_gov,
    x_mil,
    reb_no) |> 
  group_by(SideA, Year) |> 
  reframe(
    SideA, 
    Year,
    reb_no = sum(reb_no),
    reb_no_neg = sum(Negotiation),
    neg_max = max(Negotiation),
    med_max = max(Mediation),
    reb = sum(x_reb, na.rm = T),
    civ = sum(x_civ, na.rm = T),
    gov = sum(x_gov, na.rm = T),
    mil = sum(x_mil, na.rm = T)
    ) |> 
  unique()

# All militia violence
dx$all <- dx$reb + dx$civ + dx$gov + dx$mil
dx <- as.data.frame(dx)

# How many countries
length(unique(d$SideA)) 

# colors ####
dx$col <- "white"
dx$col[dx$neg_max == 1] <- "grey80"
dx$col[dx$med_max == 1] <- "grey60"
labyears <- c(1997,2013,2000,2005,2010) # years for the label

# country names: ####
countries <- unique(dx$SideA)

# directory creation
path_name <- "country-specific-graphs/"
if (!dir.exists(path_name)) {
  dir.create(path_name, recursive = TRUE)
  }

# Graphs generation ####
for(i in seq_len(length(countries))){
  
  # path. suffix:
  suffix <- ifelse(nchar(i) == 1, paste0("0", i), as.character(i))
  fn <- paste0(path_name, suffix, "_" ,countries[i], ".png")
  d <- dx[dx$SideA == countries[i],]
  ### graph save part #### 
  png(fn, width = 1800, height = 1200, res = 300, units = "px") 
  
  # graph itself
  par(mar = c(2,2,2,1), mgp = c(0,0.5,0), oma = c(0,0,0,0))
  plot(d$Year, 
       d$all, t = "n", 
       xlim = c(1997, 2013), 
       ylim = c(0, max(d$all)), 
       tck = -0.03, 
       main = unique(d$SideA), 
       xlab = "", 
       ylab = "", 
       axes = F)
  for(yy in seq_len(nrow(d))){
    polygon(
      x = c(d$Year[yy] - 0.5, d$Year[yy] + 0.5, d$Year[yy] + 0.5, d$Year[yy] - 0.5),
      y = c(0, 0, sum(d$all), sum(d$all)), col = d$col[yy], border = d$col[yy])}
  
  # lines:
  lines(d$Year, d$all, lty = 1, col = "black")
  lines(d$Year, d$reb, lty = 2, col = "red")
  lines(d$Year, d$civ, lty = 3)
  lines(d$Year, d$gov, lty = 4, col = "blue")
  
  # axes:
  axis(1, at = labyears, tck = -0.03)
  axis(1, at = setdiff(1997:2013, labyears), labels = NA, tck = -0.01)
  axis(2)
  
  # dev. off:
  dev.off()
}

# First graph with a legend #
i <- 1 # first graph
suffix <- ifelse(nchar(i) == 1, paste0("0", i), as.character(i))
fn <- paste0(path_name, suffix, "_" ,countries[i], ".png")
d <- dx[dx$SideA == countries[i],]
png(fn, width = 1800, height = 1200, res = 300, units = "px")
par(mar = c(2,2,2,1), mgp = c(0,0.5,0), oma = c(0,0,0,0))
plot(d$Year, d$all, t = "n", xlim = c(1997, 2013), ylim = c(0, max(d$all)), tck = -0.03, 
     main = unique(d$SideA), xlab = "", ylab = "", axes = F)
for(yy in seq_len(nrow(d))){
  polygon(x = c(d$Year[yy] - 0.5, d$Year[yy] + 0.5, d$Year[yy] + 0.5, d$Year[yy] - 0.5),
          y = c(0, 0, sum(d$all), sum(d$all)), col = d$col[yy], border = d$col[yy])}
lines(d$Year, d$all, lty = 1, col = "black")
lines(d$Year, d$reb, lty = 2, col = "red")
lines(d$Year, d$civ, lty = 3)
lines(d$Year, d$gov, lty = 4, col = "blue")
axis(1, at = labyears, tck = -0.03)
axis(1, at = setdiff(1997:2013, labyears), labels = NA, tck = -0.01)
axis(2)
legend("top",
       lty = c(1,2,3,4), 
       col = c("black", "red", "black", "blue"),
       legend = c("Total", "Reb.", "Civ.", "Gov."))
dev.off()

